<template>
	<view>
		<text>系统登录中...</text>
		<u-picker :closeOnClickOverlay="false" mode="selector" v-model="pickerShow" @confirm="pickerConfirm" @columnchange="columnChange"
			:range="range" range-key="fullDeptName" @close="close">
		</u-picker>
	</view>
</template>

<script>
	import {
		authLoginKey,
		deptChoose,
		qryUserDetails
	} from "@/api/public/login.js";
	import {
			mapMutations
	} from 'vuex';
	import {
		Base64
	} from "@/common/utils/base64.js";
	import UPicker from "../../uview-ui/components/u-picker/u-picker";
	export default {
		components: {
			UPicker
		},
		data() {
			return {
				// 部门选择弹窗
				pickerShow: false,
				range: [],
				key: '',
				sysUsersKey: '',
				routerPath:''
			};
		},
		onShow() {},
		onLoad(option) {
			this.login(option)
		},

		methods: {
			close() {
				return
			},
			...mapMutations(['saveDepRange']),
			login(option) {
				let auth = '';
				if (option.auth) {
					auth = JSON.parse(Base64.decode(option.auth))
				}
				this.routerPath = option.routerPath
				console.log(auth)
				this.range = auth.deptChoose;
				this.key = auth.key
				this.sysUsersKey = auth.sysUsersKey
				console.log(auth);
				if (auth.key) {
					this.tokenKeyLogin(auth.key);
				} else if (auth.sysUsersKey) {

					this.pickerShow = true;
				}
			},
			tokenKeyLogin(key) {
				authLoginKey({
						key: key
					}).then(response => {
						if (response.code == 0) {
							let data = response.data;
							this.$store.commit('setHeader', {
								"Authorization": "Bearer " + data.authToken
							});
							uni.setStorageSync('token', data.authToken)
							this.setQryUserDetails(data)
						} else {
							this.$error({
								content: response.data.message
							});
						}
					})
					.catch(response => {
						console.log(response);
					});
			},
			setQryUserDetails(data) {
				qryUserDetails().then(res1 => {
					if (res1.code == 0) {
					this.$store.commit("setHeader", {
						"Authorization": "Bearer " + data.authToken,
						'currentDeptName': encodeURIComponent(res1.data.deptInfo[0].deptName),
						'currentUserName': encodeURIComponent(res1.data.userInfo.userName),
						'deptName': encodeURIComponent(res1.data.bizDeptInfo.deptName)
					});
					res1.data.userInfo.deptNameList = res1.data.deptInfo[0].deptName
					console.log('res1.data.userInfo',res1.data.userInfo);
					this.$store.commit("setUserInfo", res1.data.userInfo);
					this.$store.commit('login')
					uni.setStorage([{
						key: 'deptinfo',
						data: res1.data.deptInfo
					}])
					if(this.routerPath == 'backlog') {
						uni.navigateTo({
							url: "/pages/backlog/backlog"
						})
					}else {
						uni.reLaunch({
							url: "/pages/index/index"
						})
					}
					
					}else {
						this.$error({
							content: res1.data.message
						});
					}
				})
			},
			columnChange(e) {},
			pickerConfirm(e) {
				const param = {
					key: this.sysUsersKey,
					userCode: this.range[e[0]].userCode
				}
				this.saveDepRange(this.range)
				deptChoose(param).then(res => {
					if (res.code === 0) {
						this.tokenKeyLogin(res.data)
					} else {
						this.$util.msg(res.message);
					}
				});
			},
		},
	};
</script>

<style lang='scss'>
</style>
